
#include <stdint.h>
#include "../../1_Application/Include/APP_WorkingNormalState.h"


#define __LQCPROCESS__
#include "../../2_Process/Include/Cmp_LQC_Process.h"
#undef __LQCPROCESS__
#include "../../2_Process/Include/Cmp_Onboarding.h"

#include "../../3_Component/Include/Cmp_KeyScan.h"
#include "../../3_Component/Include/Cmp_SetError.h"

#include "../../4_Library/Include/general.h"
#include "../../4_Library/Include/Int.h"
#include "../../4_Library/Include/Lib_UartComm.h"
#include "../../4_Library/Include/Lib_CommSubMicom.h"
#include "../../4_Library/Include/Lib_SensingADC.h"
#include "../../4_Library/Include/Lib_EEPROM.h"


#include "../../6_Vablesource/Include/Vable_ModelInformation.h"


extern const UINT8 gaucBeeperInfo[PlatformMax][BeeperInformMax];
extern const UINT8 gauCommInfo[PlatformMax][CommInformMax];
extern const UINT8 gauLqcStepInfo[PlatformMax][_STEP_MAX];


#define DelayTime 2



static UINT8 KeyValue = 0;
static UINT8 BeepStatus = 0;
static UINT8 gucLQCIndex = 0;
static UINT8 mucLogLqcStep = 0;
static UINT8 BeeperCounter = 0;
static UINT8 MainStatus = 0;

T_Time InitGapTime;
T_Time SetErrorGapTime;





static void (* const mapfnNormalLqcStepTbl[]) (void) =
{
	LQC_CHECKSUM_CheckStep,
	LQC_1888_CheckStep,
	LQC_LED_CheckStep,
	LQC_WIFI_CheckStep,
	LQC_Normal_FinalStep,
};

void Run_LQC_ModeProcess()
{
	Init_LQCTestModeProcess();

	switch(Comm_Get_KeyValue())
	{
		case NO_KEY:
			ClearKeyValue();
		break;


		case POWER_KEY:
			ClearKeyValue();
			if(ERROR_State == NO_ERROR)
			{
				LQC_Display_Power_ON_OFF();
			}
			else
			{;}
		break;

		case START_PAUSE_KEY:
			ClearKeyValue();
			if(MainStatus != 0 && ERROR_State == NO_ERROR)
			{
				TestControlStep_LQC_Normal(gucLQCIndex);
			}
			else
			{;}
		break;

		default:

			ClearKeyValue();
			if(MainStatus != 0 && ERROR_State == NO_ERROR)
			{
				OptionKeyInBeep();
			}
			else
			{;}
		break;

	}
}

void TestControlStep_LQC_Normal(UINT8 ucLQCIndex)
{
	if(0 < (sizeof(mapfnNormalLqcStepTbl) / sizeof(void*)))
	{
		mapfnNormalLqcStepTbl[ucLQCIndex]();
	}
	else
	{;}
}

void OptionKeyInBeep()
{
	if(MainStatus != 0)
	{
                CounterAddLimite(BeeperCounter, 0, 3);
		BeepStatus = ((gaucBeeperInfo[Get_JigPlatform()][Beep_HeadOnOff])\
						|((BeeperCounter & 0x03) << gaucBeeperInfo[Get_JigPlatform()][Beep_MoveBit])\
						|(gaucBeeperInfo[Get_JigPlatform()][Beep_KEY_IN] & BIN8(0001,1111)));
	}
	else
	{;}
}


void LQC_Display_Power_ON_OFF()
{
	if(MainStatus == 0)
	{
		ErrorCheckFlag = SET;
		if(gauLqcStepInfo[Get_JigPlatform()][_VERSION_STEP] == NA)
		{
			LQC_CHECKSUM_CheckStep();
		}
		else
		{
			MainStatus = gauLqcStepInfo[Get_JigPlatform()][_LQC_STEP];
			BeeperCounter++;
			mucLogLqcStep = gauLqcStepInfo[Get_JigPlatform()][Onboarding_ModelFlag == EN_ONBOARD ? _LQC_STEP : _VERSION_STEP];
			BeepStatus = ((gaucBeeperInfo[Get_JigPlatform()][Beep_HeadOnOff])\
							|((BeeperCounter & 0x03) << gaucBeeperInfo[Get_JigPlatform()][Beep_MoveBit])\
							|(gaucBeeperInfo[Get_JigPlatform()][Beep_Power_ON] & BIN8(0001,1111)));
		}
	}
	else
	{
		MainStatus = CLR;
		CounterAddLimite(BeeperCounter, 0, 3);
		mucLogLqcStep = CLR;
		BeepStatus = ((gaucBeeperInfo[Get_JigPlatform()][Beep_HeadOnOff])\
						|((BeeperCounter & 0x03) << gaucBeeperInfo[Get_JigPlatform()][Beep_MoveBit])\
						|(gaucBeeperInfo[Get_JigPlatform()][Beep_Power_OFF] & BIN8(0001,1111)));

                /*
		while(STANDBY_SIGNAL)              // why can use STANDBY_SIGNAL ??
		{;}
                */
		Standby_Signal_Flag = CLR;
		gunOpWriteFlag = CLR;
		SET_STX(CLR);
	}
}

void LQC_CHECKSUM_CheckStep()
{
	if(gauLqcStepInfo[Get_JigPlatform()][_CHECKSUM_STEP] == NA)
	{
		LQC_1888_CheckStep();
	}
	else
	{
		MainStatus = gauLqcStepInfo[Get_JigPlatform()][_CHECKSUM_STEP];
	}

	gucLQCIndex++;
}

void LQC_1888_CheckStep()
{
	if(gauLqcStepInfo[Get_JigPlatform()][_1888_STEP] == NA)
	{
		LQC_LED_CheckStep();
	}
	else
	{
		MainStatus = gauLqcStepInfo[Get_JigPlatform()][_LQC_STEP];
		mucLogLqcStep = gauLqcStepInfo[Get_JigPlatform()][_1888_STEP];
		CounterAddLimite(BeeperCounter, 0, 3);
		BeepStatus = ((gaucBeeperInfo[Get_JigPlatform()][Beep_HeadOnOff])\
						|((BeeperCounter & 0x03) << gaucBeeperInfo[Get_JigPlatform()][Beep_MoveBit])\
						|(gaucBeeperInfo[Get_JigPlatform()][Beep_KEY_IN] & BIN8(0001,1111)));
	}

	gucLQCIndex++;

}

void LQC_LED_CheckStep()
{
	if(gauLqcStepInfo[Get_JigPlatform()][_LED_STEP] == NA)
	{
		LQC_WIFI_CheckStep();
	}
	else
	{
		mucLogLqcStep = gauLqcStepInfo[Get_JigPlatform()][Onboarding_ModelFlag == EN_ONBOARD ? _VERSION_STEP : _LED_STEP];
		CounterAddLimite(BeeperCounter, 0, 3);
		BeepStatus = ((gaucBeeperInfo[Get_JigPlatform()][Beep_HeadOnOff])\
						|((BeeperCounter & 0x03) << gaucBeeperInfo[Get_JigPlatform()][Beep_MoveBit])\
						|(gaucBeeperInfo[Get_JigPlatform()][Beep_KEY_IN] & BIN8(0001,1111)));
	}
	gucLQCIndex++;
}

void LQC_WIFI_CheckStep()
{
	if(gauLqcStepInfo[Get_JigPlatform()][_WIFI_STEP] == NA)
	{
		LQC_Normal_FinalStep();
	}
	else
	{
		mucLogLqcStep = gauLqcStepInfo[Get_JigPlatform()][_WIFI_STEP];
		CounterAddLimite(BeeperCounter, 0, 3);
		BeepStatus = ((gaucBeeperInfo[Get_JigPlatform()][Beep_HeadOnOff])\
						|((BeeperCounter & 0x03) << gaucBeeperInfo[Get_JigPlatform()][Beep_MoveBit])\
						|(gaucBeeperInfo[Get_JigPlatform()][Beep_KEY_IN] & BIN8(0001,1111)));
	}
	gucLQCIndex++;

}

void LQC_Normal_FinalStep()
{
	if(gauLqcStepInfo[Get_JigPlatform()][_Final_STEP] == NA)
	{
		if((Get_WifiState() & 0x01) == SET)
		{
			LQC_Display_Power_ON_OFF();
		}
		else
		{;}
	}
	else
	{
		LQC_Display_Power_ON_OFF();
	}
}



void Init_LQCTestModeProcess()
{
	if(Comm_Get_KeyCount() == CLR)
	{
		KeyValue = 0;
		gucLQCIndex = 0;
		mucLogLqcStep = 0;
		BeeperCounter = 0;
		MainStatus = 0;
	}
	else
	{;}

	if(MainStatus == 0)
	{
		mucLogLqcStep = CLR;
		gucLQCIndex = CLR;
		BeeperCounter = CLR;
		if(Timer_MsecCheckPassTime(&InitGapTime, (DelayTime * 1000)) == SET)
		{
			BeepStatus = gaucBeeperInfo[Get_JigPlatform()][Beep_DUMMY];
		}
		else
		{;}
	}
	else
	{
		Timer_GetTime(&InitGapTime);
	}
}


void SetErrorCondition()
{
	if(ErrorCheckFlag == SET)
	{
		if(Timer_MsecCheckPassTime(&SetErrorGapTime, 10) == SET)
		{
			ErrorCheckFlag = CLR;

			if(PRE_ERROR == NO_ERROR)
			{
				;
			}
			else
			{
				MainStatus = CLR;
				ERROR_State = PRE_ERROR;
                                CounterAddLimite(BeeperCounter, 0, 3);
				BeepStatus = ((gaucBeeperInfo[Get_JigPlatform()][Beep_HeadOnOff])\
								|((BeeperCounter & 0x03) << gaucBeeperInfo[Get_JigPlatform()][Beep_MoveBit])\
								|(gaucBeeperInfo[Get_JigPlatform()][Beep_DUMMY] & BIN8(0001,1111)));
			}
		}
		else
		{
			Timer_GetTime(&SetErrorGapTime);
		}

	}
	else
	{
		;
	}
}

UINT8 Sys_GetMainStatus()
{
	return MainStatus;
}

UINT8 Comm_KeyValue()
{
	return KeyValue;
}

UINT8 Sys_GetBeepStatus()
{
	return BeepStatus;
}

UINT8 Comm_EncodeTestModeStep()
{
	return mucLogLqcStep;
}


